package com.freeLearning.boot.system.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.annotation.RepeatSubmit;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.core.security.util.SecurityUtils;
import com.freeLearning.boot.system.enums.MessageEnum;
import com.freeLearning.boot.system.model.dto.ChatMessageDTO;
import com.freeLearning.boot.system.model.entity.ConsultantUserChatMessage;
import com.freeLearning.boot.system.model.entity.Message;
import com.freeLearning.boot.system.service.ConsultantUserChatMessageService;
import com.freeLearning.boot.system.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Tag(name = "25.咨询师用户聊天接口")
@RequestMapping("/api/v1/chat")
@RestController
@RequiredArgsConstructor
public class ConsultantUserChatMessageController {
    private final ConsultantUserChatMessageService consultantUserChatMessageService;

    @GetMapping("/sendList")
    @Operation(summary = "获取当前用户发送的消息")
    public Result<List<ConsultantUserChatMessage>> getSendChatMessage() {
        Long userId = SecurityUtils.getUserId();
        List<ConsultantUserChatMessage> list = consultantUserChatMessageService.list(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getSenderId, userId)
        );
        return Result.success(list);
    }

    @GetMapping("/receiveList")
    @Operation(summary = "获取当前用户接收的消息")
    public Result<List<ConsultantUserChatMessage>> getReceiveChatMessage() {
        Long userId = SecurityUtils.getUserId();
        List<ConsultantUserChatMessage> list = consultantUserChatMessageService.list(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, userId)
        );
        return Result.success(list);
    }

    @PostMapping("/send")
    @Operation(summary = "发送消息")
    public Result<Boolean> sendChatMessage(@RequestBody ChatMessageDTO chatMessageDTO) {
        return Result.success(consultantUserChatMessageService.sendChatMessage(chatMessageDTO));
    }

    @PostMapping("/read/{senderId}")
    @Operation(summary = "标记消息为已读")
    @RepeatSubmit
    public Result<Boolean> readChatMessage(@PathVariable Long senderId) {
        boolean update = consultantUserChatMessageService.readMessage(senderId);
        return Result.judge(update);
    }

    @GetMapping("/queryNoReadMessageCount")
    @Operation(summary = "查询未读消息数量")
    public Result<Long> queryNoReadMessageCount() {
        Long userId = SecurityUtils.getUserId();
        long count = consultantUserChatMessageService.count(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, userId)
                        .eq(ConsultantUserChatMessage::getStatus, 0)
        );
        return Result.success(count);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除所有消息")
    public Result<Boolean> deleteChatMessage(@PathVariable Long id) {
        Long userId = SecurityUtils.getUserId();
        boolean remove = consultantUserChatMessageService.remove(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, id)
                        .eq(ConsultantUserChatMessage::getSenderId, userId)
        );
        boolean remove2 = consultantUserChatMessageService.remove(
                new LambdaQueryWrapper<ConsultantUserChatMessage>()
                        .eq(ConsultantUserChatMessage::getReceiverId, userId)
                        .eq(ConsultantUserChatMessage::getSenderId, id)
        );
        return Result.judge((remove && remove2));
    }
}
